﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using IdentityServer3.Core.Extensions;
using System.Threading.Tasks;
using MySSO.Core;
using MySSO.Core.Service;
using MySSO.Core.Entity;
using System.Security.Claims;

namespace EmbeddedMVC.Controllers
{
    public class LoginController : Controller
    {
        private readonly MemoryCacheManager memoryCacheManager;
        private readonly AdminUserService _adminUserService;
        private readonly WebHelper _webHelper;
        private readonly AdminVisitService _adminVisitService;
        public LoginController()
        {
            memoryCacheManager = new MemoryCacheManager();
            _adminUserService = new AdminUserService();
            _webHelper = new WebHelper();
            _adminVisitService = new AdminVisitService();
        }

        [Route("identity/custom/login")]
        public ActionResult Index(string id)
        {
            _adminVisitService.AccessRecord();
            ViewBag.id = id;
            return View();
        }

        [Route("identity/custom/login")]
        [ValidateAntiForgeryToken]
        [HttpPost]
        public ActionResult Index(string id, string password, string username)
        {
            if (string.IsNullOrWhiteSpace(password) || string.IsNullOrWhiteSpace(username))
            {
                ViewBag.Error = "用户名和密码不能为空";
                ViewBag.id = id;
                return View();
            }
            long RID = _adminVisitService.GetVistRecordId();
            int r = 0;
            string clientinfo = _webHelper.GetClientInfo();
            string ipString = _webHelper.GetCurrentIpAddress();
            //登录验证逻辑
            AdminUserEntity user = _adminUserService.Login(username, password, ipString, RID, clientinfo, out r);
            if (r == 1)
            {
                var env = Request.GetOwinContext().Environment;
                IList<Claim> claims = new List<Claim>();
                claims.Add(new Claim("Name", user.AUID.ToString()));
                env.IssueLoginCookie(new IdentityServer3.Core.Models.AuthenticatedLogin
                {
                    Subject = user.AUID.ToString(),//设置subject为用户id
                    Name = user.Name,
                    Claims= claims
                });

                var msg = env.GetSignInMessage(id);
                var returnUrl = msg.ReturnUrl;

                env.RemovePartialLoginCookie();

                return Redirect(returnUrl);


            }
            else
            {
                string errormsg = "";

                switch (r)
                {
                    case 0:
                        errormsg = "输入的Email或密码不正确！";
                        break;
                    case -1:
                        errormsg = "输入的Email或密码不正确！";
                        break;
                    case -2:
                        errormsg = "账户输入错误超过5次，被锁定24小时";
                        break;
                    case -3:
                        errormsg = "该账户已被禁用";
                        break;
                    case -4:
                        errormsg = "密码过于简单，又没有在24小时内修改，账户已被锁定。";
                        break;
                    default:
                        errormsg = "未知错误";
                        break;
                        //

                }
                ViewBag.id = id;
                ViewBag.Error = errormsg;
                return View();
            }

           
        }
    }
}